package com.zlt.appparking.utils;

import com.zlt.appparking.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class FileUploadUtil {

    public String upload(MultipartFile file,String path){//各种file
        File f = new File(path);
        if (!f.exists()){
            f.mkdirs();
        }
        String originalFilename = file.getOriginalFilename();
//        String fileName = StringUtil.uuid() + originalFilename.substring(originalFilename.lastIndexOf("."));
//        String fileUrl = "http://localhost/files/" + fileName;
        String fileUrl = "http://localhost/files/" + originalFilename;
        try {
            file.transferTo(new File(path,originalFilename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileUrl;
    }

    public String uploadQNY(MultipartFile file){
        String url = "";
        try {
            InputStream inputStream = file.getInputStream();
            // 文件名+文件后缀
            String submittedFileName = file.getOriginalFilename();
            // 有文件上传
            if (submittedFileName != null) {
                String fileName = StringUtil.uuid() + submittedFileName.substring(submittedFileName.lastIndexOf(".")); //文件名（uuid）+ 文件后缀
                url = QnyUtil.upload(inputStream, fileName);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

//    public static void main(String[] args) {
//        String path = new FileUploadUtil().getClass().getClassLoader().getResource("static/files").getPath();
//        String pa="";
//    }
}
